﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;

namespace Universe.WinForms.DesktopAPI
{
    public class Screenshot
    {
        private const uint SRCCOPY = 0xCC0020;
        [DllImport("user32.dll", SetLastError = false)]
        static extern IntPtr GetDesktopWindow();


        [DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        /// <summary>
        ///    Performs a bit-block transfer of the color data corresponding to a
        ///    rectangle of pixels from the specified source device context into
        ///    a destination device context.
        /// </summary>
        /// <param name="hdc">Handle to the destination device context.</param>
        /// <param name="nXDest">The leftmost x-coordinate of the destination rectangle (in pixels).</param>
        /// <param name="nYDest">The topmost y-coordinate of the destination rectangle (in pixels).</param>
        /// <param name="nWidth">The width of the source and destination rectangles (in pixels).</param>
        /// <param name="nHeight">The height of the source and the destination rectangles (in pixels).</param>
        /// <param name="hdcSrc">Handle to the source device context.</param>
        /// <param name="nXSrc">The leftmost x-coordinate of the source rectangle (in pixels).</param>
        /// <param name="nYSrc">The topmost y-coordinate of the source rectangle (in pixels).</param>
        /// <param name="dwRop">A raster-operation code.</param>
        /// <returns>
        ///    <c>true</c> if the operation succeeded, <c>false</c> otherwise.
        /// </returns>
        [DllImport("gdi32.dll")]
        static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth,
           int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

        [DllImport("user32.dll")]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }



        public static Rectangle GetDesktopArea()
        {
            IntPtr hDesktop = GetDesktopWindow();
            RECT r2;
            if (GetWindowRect(hDesktop, out r2))
            {
                Rectangle all = Rectangle.FromLTRB(r2.Left, r2.Top, r2.Right, r2.Bottom);
                Debug.WriteLine("Desktop RECT: " + all);
                return all;
            }

            else 
                return Rectangle.Empty;
        }
        
        public static Bitmap GetDesktopPart(Rectangle rect)
        {
            IntPtr hDesktop = GetDesktopWindow();
            Rectangle all = GetDesktopArea();

            IntPtr dcDesktop = GetWindowDC(hDesktop);

            Bitmap ret = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
            using(Graphics g = Graphics.FromImage(ret))
            {
                IntPtr dcDest = g.GetHdc();
                bool okBB = BitBlt(dcDest, 0, 0, rect.Width, rect.Height, dcDesktop, rect.Left + all.Left, rect.Top + all.Top,
                       SRCCOPY);

            }

            ReleaseDC(hDesktop, dcDesktop);

            return ret;
        }


    }
}
